package lg.minecraft.plugin.pokemonmenu.gui;

import com.cobblemon.mod.common.pokemon.Pokemon;
import lg.minecraft.plugin.library.inventory.CancelInventoryContext;
import lg.minecraft.plugin.library.inventory.ClickInventoryContext;
import lg.minecraft.plugin.library.inventory.InventoryContext;
import lg.minecraft.plugin.library.inventory.LGInventory;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.PokemonPlayer;
import lg.minecraft.plugin.pokemonmenu.event.EventFactory;
import lg.minecraft.plugin.pokemonmenu.event.PokemonToItemEvent;
import lg.minecraft.plugin.pokemonmenu.manager.PokemonPlayerManger;
import lg.minecraft.plugin.pokemonmenu.util.Position;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

/**
 * @author langle__
 * @version 1.0
 */
public class PokemonToItemInventory extends LGInventory {

    private final PokemonPlayer pokemonPlayer;
    private final Position position;

    public PokemonToItemInventory(PokemonPlayer pokemonPlayer, Position position) {
        super(3, LGPokemonMenu.getMessage().getGuiTitlePokemonToItem());
        this.pokemonPlayer = pokemonPlayer;
        this.position = position;
    }

    @Override
    public void onInventoryOpen(CancelInventoryContext context) {
        Pokemon pokemon = pokemonPlayer.getPokemon(position);

        inventory.setItem(13, LGPokemonMenu.getGuiItem().getPokemon(pokemon));
        inventory.setItem(11, LGPokemonMenu.getGuiItem().getConfirmButton(LGPokemonMenu.getMainConfig().getToItem()));
        inventory.setItem(15, LGPokemonMenu.getGuiItem().getCancelButton());
    }

    @Override
    public void onInventoryClick(ClickInventoryContext context) {
        context.setCancelled(true);
        Player player = context.getPlayer();
        int slot = context.getSlot();

        if (slot == 11) {
            Economy economy = LGPokemonMenu.getEconomy();
            int value = LGPokemonMenu.getMainConfig().getToItem();
            if (!economy.has(player, value)) {
                player.sendMessage(LGPokemonMenu.getMessage().getNotEnoughMoney());
                player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0F, 0.0F);
                return;
            }

            Pokemon pokemon = pokemonPlayer.getPokemon(position);

            PokemonToItemEvent event = EventFactory.callPokemonToItemEventEvent(player, pokemon);
            if (event.isCancelled()) {
                player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0F, 0.0F);
                return;
            }

            economy.withdrawPlayer(player, value);
            ItemStack item = LGPokemonMenu.getGuiItem().getItemPokemon(pokemon);
            PlayerInventory inventory = player.getInventory();
            inventory.addItem(item);
            pokemonPlayer.removePokemon(position);
            player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1.0F, 1.8F);
            PokemonPlayerManger.INSTANCE.getPokemonPlayer(player).getPreviousInventory().openInventory(player);
        } else if (slot == 15) {
            PokemonPlayerManger.INSTANCE.getPokemonPlayer(player).getPreviousInventory().openInventory(player);
            player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0F, 0.0F);
        }
    }

    @Override
    public void onInventoryClose(InventoryContext context) {}

    public void openInventory(Player player, LGInventory inventory) {
        PokemonPlayer pokemonPlayer = PokemonPlayerManger.INSTANCE.getPokemonPlayer(player);
        pokemonPlayer.setPreviousInventory(inventory);
        this.openInventory(player);
    }

}
